package org.zh.sh.util.http;


import com.alibaba.fastjson2.JSON;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.zh.sh.util.http.HttpStatic.*;


public class HttpUtil {
    private HttpUtil(){}

    public static final ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
    public static final HttpClient httpClient_1_1 = HttpClient.newBuilder().executor(executorService).version(HttpClient.Version.HTTP_1_1).followRedirects(HttpClient.Redirect.NEVER).connectTimeout(Duration.ofSeconds(30)).build();

    public static CompletableFuture<HttpResponse<String>> asyncPOST(String uri, Object body) {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .POST(HttpRequest.BodyPublishers.ofString(JSON.toJSONString(body)))
                .version(HttpClient.Version.HTTP_1_1)
                .header(contentType, APPLICATION_JSON_VALUE)
                .header(accept, APPLICATION_JSON_VALUE)
                .header(acceptCharset, utf8).build();
        return httpClient_1_1.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    }

    public static CompletableFuture<HttpResponse<String>> asyncGET(String uri) {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .GET()
                .version(HttpClient.Version.HTTP_1_1)
                .build();
        return httpClient_1_1.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    }

    public static CompletableFuture<HttpResponse<String>> asyncPUT(String uri, Object body) {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .PUT(HttpRequest.BodyPublishers.ofString(JSON.toJSONString(body)))
                .version(HttpClient.Version.HTTP_1_1)
                .header(contentType, APPLICATION_JSON_VALUE)
                .header(accept, APPLICATION_JSON_VALUE)
                .header(acceptCharset, Charset.defaultCharset().name()).build();
        return httpClient_1_1.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    }
    public static CompletableFuture<HttpResponse<String>> asyncHEAD(String uri,String... headers) {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .headers(headers)
                .HEAD()
                .version(HttpClient.Version.HTTP_1_1)
                .build();
        return httpClient_1_1.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    }
    public static CompletableFuture<HttpResponse<String>> asyncDELETE(String uri) {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .DELETE()
                .version(HttpClient.Version.HTTP_1_1)
                .build();
        return httpClient_1_1.sendAsync(request, HttpResponse.BodyHandlers.ofString());
    }
}
